package net.k3rnel.abugadro.providers;

import net.k3rnel.abugadro.gcodes.Comment.Comments;
import net.k3rnel.abugadro.gcodes.CommentUpdate.CommentUpdates;
import net.k3rnel.abugadro.gcodes.Issue.Issues;
import net.k3rnel.abugadro.gcodes.Label.Labels;
import net.k3rnel.abugadro.providers.Project.Projects;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;

public class ProjectsDB {

	/**
	 *
	 */
	private ProjectsDB () {
	}

	private static final ProjectsDB instance = new ProjectsDB ();

	public static ProjectsDB  getInstance() {
		return instance;
	}

	// adds a new note with a given title and text
	public void addNewProject(ContentResolver contentResolver, String name, int type,String username, String password) {
		ContentValues contentValue = new ContentValues();
		// note that we don't have to add an id as our table set id as autoincrement
		String issuesUrl = "";
		String commentsUrl = "";
		switch(type){
		case 0:
			issuesUrl = "http://code.google.com/feeds/issues/p/"+name+"/issues/full";
			commentsUrl = "http://code.google.com/feeds/issues/p/"+name+"/issues/ISSUENUM/comments/full/";
			break;
		case 1:
			issuesUrl = "http://feeds.launchpad.net/"+name+"/latest-bugs.atom";
			commentsUrl = "http://code.google.com/feeds/issues/p/"+name+"/issues/ISSUENUM/comments/full/";
			break;
		}
		contentValue.put(Projects.NAME, name);
		contentValue.put(Projects.TYPE, type);
		contentValue.put(Projects.ISSUESURL, issuesUrl);
		contentValue.put(Projects.COMMENTSURL, commentsUrl);
		contentValue.put(Projects.USERNAME, username);
		contentValue.put(Projects.PASSWORD, password);
		contentResolver.insert(Projects.CONTENT_URI, contentValue);
	}

	public int getProjectTypeById(ContentResolver contentResolver, long id) {
		int ret = 0;
		Cursor cursor = contentResolver.query(Projects.CONTENT_URI, null, Projects._ID + "='" + id + "'", null, null);
		if (null != cursor && cursor.moveToNext()) {
			int index = cursor.getColumnIndex(Projects.TYPE);
			ret = cursor.getInt(index);
		}
		cursor.close();
		return ret;
	}
	public String getLabelsByIssueId(ContentResolver contentResolver, int projId,int issueId) {
		String ret = "";
		String[] projection = {Labels.LABEL,};
		Cursor cursor = contentResolver.query(Labels.CONTENT_URI, projection, Labels.PROJECT_ID + "='" + projId + "' AND "+Labels.ISSUE_ID + "='"+issueId+"'", null, null);
		while (null != cursor && cursor.moveToNext()) {
			if(cursor.isLast())
				ret += cursor.getString(0);
			else
				ret += cursor.getString(0)+", ";
		}
		cursor.close();
		return ret;
	}
	// checks to see if a project with a given name and type is in our database
	public boolean isProjectInDB(ContentResolver contentResolver, String title, int type) {
		boolean ret = false;
		Cursor cursor = contentResolver.query(Projects.CONTENT_URI, null, Projects.NAME + "='" + title + "' AND "+Projects.TYPE + "="+type, null, null);
		if (null != cursor && cursor.moveToNext()) {
			ret = true;
		}
		cursor.close();
		return ret;
	}

	public boolean isIssueInDB(ContentResolver contentResolver, int projId, String issueId) {
		boolean ret = false;
		Cursor cursor = contentResolver.query(Issues.CONTENT_URI, null, Issues.ISSUE_ID + "='" + issueId + "' AND "+Issues.PROJECT_ID + "= '"+projId+"'", null, null);
		if (null != cursor && cursor.moveToNext()) {
			ret = true;
		}
		cursor.close();
		return ret;
	}

	public boolean isLabelInDB(ContentResolver contentResolver, int projectId, String issueId, String label) {
		boolean ret = false;
		// comment_id, proj_id, published FROM comments WHERE (issue_id='758' AND proj_id= '2' AND comment_id= '0' AND type= '0'  AND content= 'Version-4.2.5')

		Cursor cursor = contentResolver.query(Labels.CONTENT_URI, null,Labels.PROJECT_ID + "='" + projectId + "' AND "+ Labels.ISSUE_ID + "='" + issueId + "' AND "+Labels.LABEL + "= '"+label+"'", null, null);
		if (null != cursor && cursor.moveToNext()) {
			ret = true;
		}
		cursor.close();
		return ret;
	}

	public boolean isCommentInDB(ContentResolver contentResolver, int projId, int issueId, int commentId) {
		boolean ret = false;
		Cursor cursor = contentResolver.query(Comments.CONTENT_URI, null, Comments.ISSUE_ID + "='" + issueId + "' AND "+Comments.PROJECT_ID + "= '"+projId+"' AND "+Comments.COMMENT_ID + "= '"+commentId+"'", null, null);
		if (null != cursor && cursor.moveToNext()) {
			ret = true;
		}
		cursor.close();
		return ret;
	}

	public boolean isCommentUpdateInDB(ContentResolver contentResolver, int projId, int issueId, int commentId, String content, int type) {
		boolean ret = false;
		Cursor cursor = contentResolver.query(CommentUpdates.CONTENT_URI, null, CommentUpdates.ISSUE_ID + "='" + issueId + "' AND "+CommentUpdates.PROJECT_ID + "= '"+projId+"' AND "+CommentUpdates.COMMENT_ID + "= '"+commentId+"' AND "+CommentUpdates.TYPE + "= '"+type+"'  AND "+CommentUpdates.CONTENT + "= '"+content+"'", null, null);
		if (null != cursor && cursor.moveToNext()) {
			ret = true;
		}
		cursor.close();
		return ret;
	}
	public void updateProjectFromTitle(ContentResolver contentResolver, String projName, int projType, String username, String password) {
		ContentValues contentValue = new ContentValues();
		contentValue.put(Projects.USERNAME, username);
		contentValue.put(Projects.PASSWORD, password);
		contentResolver.update(Projects.CONTENT_URI, contentValue, Projects.NAME + "='" + projName + "' AND "+Projects.TYPE+"='"+projType+"'", null);
	}

	//    // erases all entries in the database
	public void refreshCache(ContentResolver contentResolver) {
		contentResolver.delete(Issues.CONTENT_URI, null, null);
		contentResolver.delete(Labels.CONTENT_URI, null, null);

	}

	public Cursor getIssues(ContentResolver contentResolver, Uri projectUri, String[] projection) {
		Cursor cursor = contentResolver.query(projectUri, projection,null,null,null);
		return cursor;
	
	}

}
